## Replication Files for Kingzette et al. 
## "How Affective Polarization Undermines Support for Democratic Norms" 
## Public Opinion Quarterly 
## This Version: December 2020 

# Load necessary packages 
library(scales)
library(stargazer)
library(tidyverse)
library(psych)
library(haven)
library(ggpubr)
library(foreign)
library(corrr)
library(tableHTML)

## Read in the data --> NOTE YOU MUST YOUR WORKING DIRECTORY HERE 
#setwd("SET TO YOUR WORKING DIRECTORY")
norms <- read.csv("normsdata.csv")

############################
## Data Prep and Cleaning ##
############################

# First looking at democratic norms items to check for missingness
summary(norms$methods)
summary(norms$checks)
summary(norms$exec_power)
summary(norms$institu_respect)
summary(norms$vote)
summary(norms$political_voice)
summary(norms$inherent_unequal)
summary(norms$protest)

# It looks like the same 1320 people or so did not answer any items on scale, 
# so removing from data set here (they did not take second wave of survey)
norms <- norms %>%
  filter(!is.na(methods) & !is.na(checks) & !is.na(exec_power) & 
           !is.na(institu_respect) & !is.na(vote) & !is.na(political_voice) &
           !is.na(inherent_unequal) & !is.na(protest))

# Creating a single scale with norms items 
# Making all items such that 5 indicates strong support for norm and
# 1 indicates least support for norm
norms <- norms %>%
  mutate(methods = 6 - methods,
         exec_power = 6 - exec_power,
         inherent_unequal = 6 - inherent_unequal,
         protest = 6 - protest)

# going to dichotomize the norms items -- 1 indicates agreement
# with direction of norm while 0 indicates otherwise
# (choice to dichotomize made after looking at reliability w/ slightly 
# different operationalizations)
norms <- norms %>%
  mutate(methods_d = ifelse(methods == 4 | methods == 5, 1, 0),
        exec_power_d = ifelse(exec_power == 4 | exec_power == 5, 1, 0),
        inherent_unequal_d = ifelse(inherent_unequal == 4 | inherent_unequal == 5, 1, 0),
        protest_d = ifelse(protest == 4 | protest == 5, 1, 0),
        checks_d = ifelse(checks == 4 | checks == 5, 1, 0),
        institu_respect_d = ifelse(institu_respect == 4 | institu_respect == 5, 1, 0),
        vote_d = ifelse(vote == 4 | vote == 5, 1, 0),
        political_voice_d = ifelse(political_voice == 4 | political_voice == 5, 1, 0))


# Checking Cronbach's alpha on these items with tetrachloric correlations
# because tetrachloric corrs are more appropriate to dichotomous data
tetra_cors <- norms %>%
  select(methods_d, checks_d, exec_power_d, institu_respect_d,
                    vote_d, political_voice_d,inherent_unequal_d, protest_d) %>%
  tetrachoric()

psych::alpha(tetra_cors$rho)

# correlation matrix of items
tetra_cors$rho

# Creating in-group bias measure
norms <- norms %>%
  # Reverse Coding OutGroup Traits
  mutate(fliphypocout = 6 - hypocritical_outgroup,
         flipselfishout = 6 - selfish_outgroup,
         flipmeanout = 6 - mean_outgroup,
         flipmarry_outgroup = 5 - marry_outgroup) %>%
  # Putting Everything on a 0-1 Scale for Outgroup
  mutate(feelingtherm_outgroup = (feelingtherm_outgroup/100),
         patriotic_outgroup = (patriotic_outgroup-1)/4,
         intelligent_outgroup = (intelligent_outgroup-1)/4,
         honest_outgroup = (honest_outgroup-1)/4,
         openminded_outgroup = (openminded_outgroup-1)/4,
         generous_outgroup = (generous_outgroup -1)/4,
         fliphypocout =(fliphypocout-1)/4,
         flipselfishout = (flipselfishout-1)/4,
         flipmeanout =(flipmeanout-1)/4,
         trust_outgroup = (trust_outgroup-1)/4,
         friends_outgroup =(friends_outgroup-1)/3,
         neighbors_outgroup = (neighbors_outgroup-1)/3,
         flipmarry_outgroup =(flipmarry_outgroup-1)/3,
         allout = (feelingtherm_outgroup + patriotic_outgroup + 
                     intelligent_outgroup + honest_outgroup + 
                     openminded_outgroup + generous_outgroup + 
                     fliphypocout + flipselfishout + flipmeanout + 
                     trust_outgroup + friends_outgroup + 
                     neighbors_outgroup + flipmarry_outgroup)/13,
         allout = scales::rescale(allout)) %>%
  # Creating aggregate measure of feelings toward ingroup in same way as feelings toward outgroup
  mutate(fliphypocin = 6 - hypocritical_ingroup,
         flipselfishin = 6 - selfish_ingroup,
         flipmeanin = 6 - mean_ingroup,
         flipmarry_ingroup = 5 - marry_ingroup) %>%
  mutate(feelingtherm_ingroup = (feelingtherm_ingroup/100),
         patriotic_ingroup = (patriotic_ingroup-1)/4,
         intelligent_ingroup = (intelligent_ingroup-1)/4,
         honest_ingroup = (honest_ingroup-1)/4,
         openminded_ingroup = (openminded_ingroup-1)/4,
         generous_ingroup = (generous_ingroup -1)/4,
         fliphypocin =(fliphypocin-1)/4,
         flipselfishin = (flipselfishin-1)/4,
         flipmeanin =(flipmeanin-1)/4,
         trust_ingroup = (trust_ingroup-1)/4,
         friends_ingroup =(friends_ingroup-1)/3,
         neighbors_ingroup = (neighbors_ingroup-1)/3,
         flipmarry_ingroup =(flipmarry_ingroup-1)/3,
         allin = (feelingtherm_ingroup + patriotic_ingroup + 
                    intelligent_ingroup + honest_ingroup + 
                    openminded_ingroup + generous_ingroup + 
                    fliphypocin + flipselfishin + flipmeanin + 
                    trust_ingroup + friends_ingroup + 
                    neighbors_ingroup + flipmarry_ingroup)/13,
         allin = scales::rescale(allin),
         # Creating ingroup bias measure
         ingroup_bias = allin - allout)

# finding cronbach's alpha of in-group bias measure
norms %>%
  select(feelingtherm_ingroup, patriotic_ingroup, intelligent_ingroup, honest_ingroup, 
         openminded_ingroup, generous_ingroup,fliphypocin, flipselfishin, flipmeanin,
         trust_ingroup, friends_ingroup, neighbors_ingroup, flipmarry_ingroup,
         feelingtherm_outgroup, patriotic_outgroup, intelligent_outgroup, honest_outgroup, 
         openminded_outgroup, generous_outgroup, fliphypocout, flipselfishout, flipmeanout,
         trust_outgroup, friends_outgroup, neighbors_outgroup, flipmarry_outgroup) %>%
  psych::alpha(check.keys = TRUE)

summary(norms$ingroup_bias)

# Rescaling bias measure to go from 0 to 1
norms$ingroup_bias <- scales::rescale(norms$ingroup_bias)

# creating correlation matrix of these items
norms %>%
  select(feelingtherm_outgroup, patriotic_outgroup, intelligent_outgroup, honest_outgroup, 
         openminded_outgroup, generous_outgroup,fliphypocout, flipselfishout, flipmeanout,
         trust_outgroup, friends_outgroup, neighbors_outgroup, flipmarry_outgroup,
         feelingtherm_ingroup, patriotic_ingroup, intelligent_ingroup, honest_ingroup, 
         openminded_ingroup, generous_ingroup,fliphypocin, flipselfishin, flipmeanin,
         trust_ingroup, friends_ingroup, neighbors_ingroup, flipmarry_ingroup) %>%
  na.omit() %>%
  cor()


# Creating other variables we should control for: 1) education; 2) religion as fixed effect
norms <- norms %>%
  # Political knowledge scale from 0 to 5 based on number of Qs answered correctly
  mutate(p_knowledge_veto = ifelse(is.na(p_knowledge_veto) == TRUE, 0, 
                                   p_knowledge_veto),
         p_knowledge_hofr = ifelse(is.na(p_knowledge_hofr) == TRUE, 0, 
                                   p_knowledge_hofr),
         p_knowledge_constitution = ifelse(is.na(p_knowledge_constitution) == TRUE, 
                                           0, p_knowledge_constitution),
         p_knowledge_vp = ifelse(is.na(p_knowledge_vp) == TRUE, 0,
                                 p_knowledge_vp),
         p_knowledge_conservative = ifelse(is.na(p_knowledge_conservative) == TRUE,
                                           0, p_knowledge_conservative),
         know1 = ifelse(p_knowledge_veto == 4, 1, 0),
         know2 = ifelse(p_knowledge_hofr == 1, 1, 0),
         know3 = ifelse(p_knowledge_constitution == 3, 1, 0),
         know4 = ifelse(p_knowledge_vp == 3, 1, 0),
         know5 = ifelse(p_knowledge_conservative == 2, 1, 0),
         knowledge = know1 + know2 + know3 + know4 + know5,
         
         # Indicator variables for religious groups
         protestant = if_else(religion == 1,1,0),
         catholic = if_else(religion == 2,1,0),
         jewish  = if_else(religion == 3,1,0),
         other = if_else(religion == 4 | religion == 5 | religion == 6,1,0),
         not_relig = if_else(religion == 7,1,0),
         
         # Indicator variable for non-Hispanic white
         white = ifelse(race == 1, 1, 0),
         
         # Indicator variable for female
         female = ifelse(gender == 2, 1, 
                         ifelse(gender == 1 |
                                  gender == 3 |
                                  gender == 4, 0, NA)),

         #Three-factor party id (using wave 1) so we can divide out partisans
         pid3 = ifelse(wave1pid == 1 | wave1pid == 2 | wave1pid == 3, "Democrat",
                       ifelse(wave1pid == 7 | wave1pid == 6 | wave1pid == 5, "Republican", 
                              ifelse(wave1pid == 4, "Independent", NA))))

# Creating measure of ideology based on policy positions
policy_pca <- norms %>%
  select(soc_sec_spending, healthcare, services_spending, gov_role,
         abortion, homosexual_protection, mmigration, trans_protection) %>%
  na.omit() %>%
  prcomp(scale. = TRUE,
         center = TRUE)
summary(policy_pca) # pretty indicative data is mostly on one dimension

# Adding policy liberalism measure to data set
norms <- norms %>%
  filter(!is.na(soc_sec_spending) & !is.na(healthcare) & !is.na(services_spending) & 
           !is.na(gov_role) & !is.na(abortion) & !is.na(homosexual_protection) &
           !is.na(mmigration) & !is.na(trans_protection)) %>%
  mutate(conservatism = scales::rescale(-policy_pca$x[,1]))
           
# Now going to subset to all partisans because it does not make sense to think
# of an out-group for true independents. In process, will subset by only those 
# who answered all the affective polarization questions (not NA on composite)
partisans <- norms %>%
  filter(pid3 == "Democrat" | pid3 == "Republican") %>%
  filter(!is.na(ingroup_bias)) %>%
  # Creating binary factor for republican for interaction models
  mutate(republican = ifelse(pid3 == "Republican", 1, 0))

########################################
## Table 1: Mean/SD of Each Norm Item ## 
########################################

# Finding means and sds on each norms item (Table 1 in paper)
partisans %>%
  select(methods, checks, exec_power, institu_respect,
         vote, political_voice, inherent_unequal, protest) %>%
  psych::describe() %>%
  as_tibble(rownames="rowname") %>%
  print()

################################
## Table 2: Regression Models ##
################################

# Running models for Table 2 in paper
methods_modideo <- lm(methods ~ republican + ingroup_bias + knowledge + conservatism +
                        education + white + female + protestant + 
                        catholic + jewish + not_relig + republican*ingroup_bias,
                      data = partisans)
summary(methods_modideo)

checks_modideo <- lm(checks ~ republican + ingroup_bias + knowledge + conservatism +
                       education + white + female + protestant + 
                       catholic + jewish + not_relig + republican*ingroup_bias,
                     data = partisans)
summary(checks_modideo)

exec_power_modideo <- lm(exec_power ~ republican + ingroup_bias + knowledge + conservatism +
                           education + white + female + protestant + 
                           catholic + jewish + not_relig + republican*ingroup_bias,
                         data = partisans)
summary(exec_power_modideo)

institu_respect_modideo <- lm(institu_respect ~ republican + ingroup_bias + knowledge + conservatism +
                                education + white + female + protestant + 
                                catholic + jewish + not_relig + republican*ingroup_bias,
                              data = partisans)
summary(institu_respect_modideo)

vote_modideo <- lm(vote ~ republican + ingroup_bias + knowledge + conservatism +
                     education + white + female + protestant + 
                     catholic + jewish + not_relig + republican*ingroup_bias,
                   data = partisans)
summary(vote_modideo)

political_voice_modideo <- lm(political_voice ~ republican + ingroup_bias + knowledge + conservatism +
                                education + white + female + protestant + 
                                catholic + jewish + not_relig + republican*ingroup_bias,
                              data = partisans)
summary(political_voice_modideo)

inherent_unequal_modideo <- lm(inherent_unequal ~ republican + ingroup_bias + knowledge + conservatism +
                                 education + white + female + protestant + 
                                 catholic + jewish + not_relig + republican*ingroup_bias,
                               data = partisans)
summary(inherent_unequal_modideo)

protest_modideo <- lm(protest ~ republican + ingroup_bias + knowledge + conservatism +
                        education + white + female + protestant + 
                        catholic + jewish + not_relig + republican*ingroup_bias,
                      data = partisans)
summary(protest_modideo)

stargazer(methods_modideo, checks_modideo, exec_power_modideo, institu_respect_modideo,
          vote_modideo, political_voice_modideo, inherent_unequal_modideo, protest_modideo,
          type = "html",
          covariate.labels = c("Republican",
                               "In-group Bias",
                               "Political Knowledge",
                               "Conservatism",
                               "Education",
                               "Non-Hispanic White",
                               "Female",
                               "Religion: Protestant",
                               "Religion: Catholic",
                               "Religion: Jewish",
                               "Religion: None",
                               "Rep.* In-group Bias"),
          out = "allmodsideo.htm")


##############
## Figure 1 ## 
############## 

# adding interaction with political knowledge
methods_modideo2 <- lm(methods ~ republican + ingroup_bias + knowledge + conservatism +
                         education + white + female + protestant + 
                         catholic + jewish + not_relig + republican*ingroup_bias*knowledge,
                       data = partisans)
summary(methods_modideo2)

checks_modideo2 <- lm(checks ~ republican + ingroup_bias + knowledge + conservatism +
                        education + white + female + protestant + 
                        catholic + jewish + not_relig + republican*ingroup_bias*knowledge,
                      data = partisans)
summary(checks_modideo2)

exec_power_modideo2 <- lm(exec_power ~ republican + ingroup_bias + knowledge + conservatism +
                            education + white + female + protestant + 
                            catholic + jewish + not_relig + republican*ingroup_bias*knowledge,
                          data = partisans)
summary(exec_power_modideo2)

institu_respect_modideo2 <- lm(institu_respect ~ republican + ingroup_bias + knowledge + conservatism +
                                 education + white + female + protestant + 
                                 catholic + jewish + not_relig + republican*ingroup_bias*knowledge,
                               data = partisans)
summary(institu_respect_modideo2)

vote_modideo2 <- lm(vote ~ republican + ingroup_bias + knowledge + conservatism +
                      education + white + female + protestant + 
                      catholic + jewish + not_relig + republican*ingroup_bias*knowledge,
                    data = partisans)
summary(vote_modideo2)

political_voice_modideo2 <- lm(political_voice ~ republican + ingroup_bias + knowledge + conservatism +
                                 education + white + female + protestant + 
                                 catholic + jewish + not_relig + republican*ingroup_bias*knowledge,
                               data = partisans)
summary(political_voice_modideo2)

inherent_unequal_modideo2 <- lm(inherent_unequal ~ republican + ingroup_bias + knowledge + conservatism +
                                  education + white + female + protestant + 
                                  catholic + jewish + not_relig + republican*ingroup_bias*knowledge,
                                data = partisans)
summary(inherent_unequal_modideo2)

protest_modideo2 <- lm(protest ~ republican + ingroup_bias + knowledge + conservatism +
                         education + white + female + protestant + 
                         catholic + jewish + not_relig + republican*ingroup_bias*knowledge,
                       data = partisans)
summary(protest_modideo2)

stargazer(methods_modideo2, checks_modideo2, exec_power_modideo2, institu_respect_modideo2,
          vote_modideo2, political_voice_modideo2, inherent_unequal_modideo2, protest_modideo2,
          type = "html",
          covariate.labels = c("Republican",
                               "In-group Bias",
                               "Political Knowledge",
                               "Conservatism",
                               "Education",
                               "Non-Hispanic White",
                               "Female",
                               "Religion: Protestant",
                               "Religion: Catholic",
                               "Religion: Jewish",
                               "Religion: None",
                               "Rep.* In-group Bias",
                               "Rep.* Knowledge", 
                               "In-group Bias* Knowledge",
                               "Rep.* In-group Bias* Knowledge"),
          out = "allmodsideo2.htm")

# Make figures for the knowledge interaction 

## Set some graphical parameters 
title.size <- 12 
text.size <- 12 
legend.size <- 11 

## Actually make the figures (by panel)
fig1 <- partisans %>%
  filter(!is.na(republican) & !is.na(conservatism) &
           !is.na(knowledge) & !is.na(education) & !is.na(female) &
           !is.na(white) & !is.na(protestant) & !is.na(catholic) & 
           !is.na(jewish) & !is.na(not_relig) & !is.na(ingroup_bias)) %>%
  filter(knowledge == 1 | knowledge == 3 | knowledge == 5) %>%
  mutate(preds = coef(methods_modideo2)[1] + republican*coef(methods_modideo2)[2] +
           ingroup_bias*coef(methods_modideo2)[3] + knowledge*coef(methods_modideo2)[4] + 
           median(conservatism)*coef(methods_modideo2)[5] +
           median(education)*coef(methods_modideo2)[6] +
           median(white)*coef(methods_modideo2)[7] + median(female)*coef(methods_modideo2)[8] + 
           median(protestant)*coef(methods_modideo2)[9] + median(catholic)*coef(methods_modideo2)[10] +
           median(jewish)*coef(methods_modideo2)[11] + median(not_relig)*coef(methods_modideo2)[12] +
           republican*ingroup_bias*coef(methods_modideo2)[13] +
           republican*knowledge*coef(methods_modideo2)[14] + 
           ingroup_bias*knowledge*coef(methods_modideo2)[15] +
           republican*ingroup_bias*knowledge*coef(methods_modideo2)[16]) %>%
  ggplot(aes(x = ingroup_bias, 
             y = preds,
             colour = as.factor(republican),
             lty = as.factor(knowledge)))+
  geom_smooth(method = "lm", 
              se = FALSE)+
  ggtitle("Get Things Done")+
  labs(x = "Affective Polarization", 
       y = "Support for Democratic Norm",
       col = "Partisan Identification",
       linetype = "Political Knowledge") +
  scale_x_continuous(limits = c(0,1), 
                     labels = c("Low",
                                "High"),
                     breaks = c(0,1)) +
  scale_linetype_manual(values = c("dotted", "dashed", "solid"),
                        labels = c("Low", "Medium", "High"))+
  scale_colour_grey(breaks = c(0,1),
                    labels = c("Democrats", "Republicans"),
                    start = .1, end = .6)+
  theme_bw()+
  theme(axis.text.y = element_blank(),
        axis.text.x = element_text(size=text.size),
        axis.title = element_text(size =text.size),
        axis.title.y = element_blank(), 
        axis.ticks.y = element_blank(),
        title = element_text(size = title.size),
        legend.text = element_text(size = legend.size)) +
  guides(lty = guide_legend(override.aes = list(col = 'black')))


fig2 <- partisans %>%
  filter(!is.na(republican) & !is.na(conservatism) &
           !is.na(knowledge) & !is.na(education) & !is.na(female) &
           !is.na(white) & !is.na(protestant) & !is.na(catholic) & 
           !is.na(jewish) & !is.na(not_relig) & !is.na(ingroup_bias)) %>%
  filter(knowledge == 1 | knowledge == 3 | knowledge == 5) %>%
  mutate(preds = coef(checks_modideo2)[1] + republican*coef(checks_modideo2)[2] +
           ingroup_bias*coef(checks_modideo2)[3] + knowledge*coef(checks_modideo2)[4] + 
           median(conservatism)*coef(checks_modideo2)[5] +
           median(education)*coef(checks_modideo2)[6] +
           median(white)*coef(checks_modideo2)[7] + median(female)*coef(checks_modideo2)[8] + 
           median(protestant)*coef(checks_modideo2)[9] + median(catholic)*coef(checks_modideo2)[10] +
           median(jewish)*coef(checks_modideo2)[11] + median(not_relig)*coef(checks_modideo2)[12] +
           republican*ingroup_bias*coef(checks_modideo2)[13] +
           republican*knowledge*coef(checks_modideo2)[14] + 
           ingroup_bias*knowledge*coef(checks_modideo2)[15] +
           republican*ingroup_bias*knowledge*coef(checks_modideo2)[16]) %>%
  ggplot(aes(x = ingroup_bias, 
             y = preds, 
             colour = as.factor(republican)))+
  geom_smooth(aes(linetype = as.factor(knowledge)), method = "lm", se = FALSE)+
  ggtitle("Checks and Balances")+
  labs(x = "Affective Polarization", 
       y = "Support for Democratic Norm",
       col = "Partisan Identification",
       linetype = "Political Knowledge") +
  scale_x_continuous(limits = c(0,1), 
                     labels = c("Low",
                                "High"),
                     breaks = c(0,1)) +
  scale_colour_grey(breaks = c(0,1),
                    labels = c("Democrats", "Republicans"),
                    start = .1, end = .6)+
  scale_linetype_manual(values = c("dotted", "dashed", "solid"),
                        labels = c("Low", "Medium", "High"))+
  theme_bw()+
  theme(axis.text.y = element_blank(),
        axis.text.x = element_text(size=text.size),
        axis.title = element_text(size =text.size),
        axis.title.y = element_blank(), 
        axis.ticks.y = element_blank(),
        title = element_text(size = title.size),
        legend.text = element_text(size = legend.size)) +
  guides(lty = guide_legend(override.aes = list(col = 'black')))

fig3 <- partisans %>%
  filter(!is.na(republican) & !is.na(conservatism) &
           !is.na(knowledge) & !is.na(education) & !is.na(female) &
           !is.na(white) & !is.na(protestant) & !is.na(catholic) & 
           !is.na(jewish) & !is.na(not_relig) & !is.na(ingroup_bias)) %>%
  filter(knowledge == 1 | knowledge == 3 | knowledge == 5) %>%
  mutate(preds = coef(exec_power_modideo2)[1] + republican*coef(exec_power_modideo2)[2] +
           ingroup_bias*coef(exec_power_modideo2)[3] + knowledge*coef(exec_power_modideo2)[4] + 
           median(conservatism)*coef(exec_power_modideo2)[5] +
           median(education)*coef(exec_power_modideo2)[6] +
           median(white)*coef(exec_power_modideo2)[7] + median(female)*coef(exec_power_modideo2)[8] + 
           median(protestant)*coef(exec_power_modideo2)[9] + median(catholic)*coef(exec_power_modideo2)[10] +
           median(jewish)*coef(exec_power_modideo2)[11] + median(not_relig)*coef(exec_power_modideo2)[12] +
           republican*ingroup_bias*coef(exec_power_modideo2)[13] +
           republican*knowledge*coef(exec_power_modideo2)[14] + 
           ingroup_bias*knowledge*coef(exec_power_modideo2)[15] +
           republican*ingroup_bias*knowledge*coef(exec_power_modideo2)[16]) %>%
  ggplot(aes(x = ingroup_bias, 
             y = preds, 
             colour = as.factor(republican)))+
  geom_smooth(aes(linetype = as.factor(knowledge)), method = "lm", se = FALSE)+
  ggtitle("Executive Power")+
  labs(x = "Affective Polarization", 
       y = "Support for Democratic Norm",
       col = "Partisan Identification",
       linetype = "Political Knowledge") +
  scale_x_continuous(limits = c(0,1), 
                     labels = c("Low",
                                "High"),
                     breaks = c(0,1)) +
  scale_colour_grey(breaks = c(0,1),
                    labels = c("Democrats", "Republicans"),
                    start = .1, end = .6)+
  scale_linetype_manual(values = c("dotted", "dashed", "solid"),
                        labels = c("Low", "Medium", "High"))+
  theme_bw()+
  theme(axis.text.y = element_blank(),
        axis.text.x = element_text(size=text.size),
        axis.title = element_text(size =text.size),
        axis.title.y = element_blank(), 
        axis.ticks.y = element_blank(),
        title = element_text(size = title.size),
        legend.text = element_text(size = legend.size)) +
  guides(lty = guide_legend(override.aes = list(col = 'black')))


fig4 <- partisans %>%
  filter(!is.na(republican) & !is.na(conservatism) &
           !is.na(knowledge) & !is.na(education) & !is.na(female) &
           !is.na(white) & !is.na(protestant) & !is.na(catholic) & 
           !is.na(jewish) & !is.na(not_relig) & !is.na(ingroup_bias)) %>%
  filter(knowledge == 1 | knowledge == 3 | knowledge == 5) %>%
  mutate(preds = coef(institu_respect_modideo2)[1] + republican*coef(institu_respect_modideo2)[2] +
           ingroup_bias*coef(institu_respect_modideo2)[3] + knowledge*coef(institu_respect_modideo2)[4] + 
           median(conservatism)*coef(institu_respect_modideo2)[5] +
           median(education)*coef(institu_respect_modideo2)[6] +
           median(white)*coef(institu_respect_modideo2)[7] + median(female)*coef(institu_respect_modideo2)[8] + 
           median(protestant)*coef(institu_respect_modideo2)[9] + median(catholic)*coef(institu_respect_modideo2)[10] +
           median(jewish)*coef(institu_respect_modideo2)[11] + median(not_relig)*coef(institu_respect_modideo2)[12] +
           republican*ingroup_bias*coef(institu_respect_modideo2)[13] +
           republican*knowledge*coef(institu_respect_modideo2)[14] + 
           ingroup_bias*knowledge*coef(institu_respect_modideo2)[15] +
           republican*ingroup_bias*knowledge*coef(institu_respect_modideo2)[16]) %>%
  ggplot(aes(x = ingroup_bias, 
             y = preds, 
             colour = as.factor(republican)))+
  geom_smooth(aes(linetype = as.factor(knowledge)), method = "lm", se = FALSE)+
  ggtitle("Respect for Institutions")+
  labs(x = "Affective Polarization", 
       y = "Support for Democratic Norm",
       col = "Partisan Identification",
       linetype = "Political Knowledge") +
  scale_x_continuous(limits = c(0,1), 
                     labels = c("Low",
                                "High"),
                     breaks = c(0,1)) +
  scale_colour_grey(breaks = c(0,1),
                    labels = c("Democrats", "Republicans"),
                    start = .1, end = .6)+
  scale_linetype_manual(values = c("dotted", "dashed", "solid"),
                        labels = c("Low", "Medium", "High"))+
  theme_bw()+
  theme(axis.text.y = element_blank(),
        axis.text.x = element_text(size=text.size),
        axis.title = element_text(size =text.size),
        axis.title.y = element_blank(), 
        axis.ticks.y = element_blank(),
        title = element_text(size = title.size),
        legend.text = element_text(size = legend.size)) +
  guides(lty = guide_legend(override.aes = list(col = 'black')))

fig5 <- partisans %>%
  filter(!is.na(republican) & !is.na(conservatism) &
           !is.na(knowledge) & !is.na(education) & !is.na(female) &
           !is.na(white) & !is.na(protestant) & !is.na(catholic) & 
           !is.na(jewish) & !is.na(not_relig) & !is.na(ingroup_bias)) %>%
  filter(knowledge == 1 | knowledge == 3 | knowledge == 5) %>%
  mutate(preds = coef(vote_modideo2)[1] + republican*coef(vote_modideo2)[2] +
           ingroup_bias*coef(vote_modideo2)[3] + knowledge*coef(vote_modideo2)[4] + 
           median(conservatism)*coef(vote_modideo2)[5] +
           median(education)*coef(vote_modideo2)[6] +
           median(white)*coef(vote_modideo2)[7] + median(female)*coef(vote_modideo2)[8] + 
           median(protestant)*coef(vote_modideo2)[9] + median(catholic)*coef(vote_modideo2)[10] +
           median(jewish)*coef(vote_modideo2)[11] + median(not_relig)*coef(vote_modideo2)[12] +
           republican*ingroup_bias*coef(vote_modideo2)[13] +
           republican*knowledge*coef(vote_modideo2)[14] + 
           ingroup_bias*knowledge*coef(vote_modideo2)[15] +
           republican*ingroup_bias*knowledge*coef(vote_modideo2)[16]) %>%
  ggplot(aes(x = ingroup_bias, 
             y = preds, 
             colour = as.factor(republican)))+
  geom_smooth(aes(linetype = as.factor(knowledge)), method = "lm", se = FALSE)+
  ggtitle("Misinformed Vote")+
  labs(x = "Affective Polarization", 
       y = "Support for Democratic Norm",
       col = "Partisan Identification",
       linetype = "Political Knowledge") +
  scale_x_continuous(limits = c(0,1), 
                     labels = c("Low",
                                "High"),
                     breaks = c(0,1)) +
  scale_colour_grey(breaks = c(0,1),
                    labels = c("Democrats", "Republicans"),
                    start = .1, end = .6)+
  scale_linetype_manual(values = c("dotted", "dashed", "solid"),
                        labels = c("Low", "Medium", "High"))+
  theme_bw()+
  theme(axis.text.y = element_blank(),
        axis.text.x = element_text(size=text.size),
        axis.title = element_text(size =text.size),
        axis.title.y = element_blank(), 
        axis.ticks.y = element_blank(),
        title = element_text(size = title.size),
        legend.text = element_text(size = legend.size)) +
  guides(lty = guide_legend(override.aes = list(col = 'black')))

fig6 <- partisans %>%
  filter(!is.na(republican) & !is.na(conservatism) &
           !is.na(knowledge) & !is.na(education) & !is.na(female) &
           !is.na(white) & !is.na(protestant) & !is.na(catholic) & 
           !is.na(jewish) & !is.na(not_relig) & !is.na(ingroup_bias)) %>%
  filter(knowledge == 1 | knowledge == 3 | knowledge == 5) %>%
  mutate(preds = coef(political_voice_modideo2)[1] + republican*coef(political_voice_modideo2)[2] +
           ingroup_bias*coef(political_voice_modideo2)[3] + knowledge*coef(political_voice_modideo2)[4] + 
           median(conservatism)*coef(political_voice_modideo2)[5] +
           median(education)*coef(political_voice_modideo2)[6] +
           median(white)*coef(political_voice_modideo2)[7] + median(female)*coef(political_voice_modideo2)[8] + 
           median(protestant)*coef(political_voice_modideo2)[9] + median(catholic)*coef(political_voice_modideo2)[10] +
           median(jewish)*coef(political_voice_modideo2)[11] + median(not_relig)*coef(political_voice_modideo2)[12] +
           republican*ingroup_bias*coef(political_voice_modideo2)[13] +
           republican*knowledge*coef(political_voice_modideo2)[14] + 
           ingroup_bias*knowledge*coef(political_voice_modideo2)[15] +
           republican*ingroup_bias*knowledge*coef(political_voice_modideo2)[16]) %>%
  ggplot(aes(x = ingroup_bias, 
             y = preds, 
             colour = as.factor(republican)))+
  geom_smooth(aes(linetype = as.factor(knowledge)), method = "lm", se = FALSE)+
  ggtitle("Opponents Get Voice")+
  labs(x = "Affective Polarization", 
       y = "Support for Democratic Norm",
       col = "Partisan Identification",
       linetype = "Political Knowledge") +
  scale_x_continuous(limits = c(0,1), 
                     labels = c("Low",
                                "High"),
                     breaks = c(0,1)) +
  scale_colour_grey(breaks = c(0,1),
                    labels = c("Democrats", "Republicans"),
                    start = .1, end = .6)+
  scale_linetype_manual(values = c("dotted", "dashed", "solid"),
                        labels = c("Low", "Medium", "High"))+
  theme_bw()+
  theme(axis.text.y = element_blank(),
        axis.text.x = element_text(size=text.size),
        axis.title = element_text(size =text.size),
        axis.title.y = element_blank(), 
        axis.ticks.y = element_blank(),
        title = element_text(size = title.size),
        legend.text = element_text(size = legend.size)) +
  guides(lty = guide_legend(override.aes = list(col = 'black')))


fig7 <- partisans %>%
  filter(!is.na(republican) & !is.na(conservatism) &
           !is.na(knowledge) & !is.na(education) & !is.na(female) &
           !is.na(white) & !is.na(protestant) & !is.na(catholic) & 
           !is.na(jewish) & !is.na(not_relig) & !is.na(ingroup_bias)) %>%
  filter(knowledge == 1 | knowledge == 3 | knowledge == 5) %>%
  mutate(preds = coef(inherent_unequal_modideo2)[1] + republican*coef(inherent_unequal_modideo2)[2] +
           ingroup_bias*coef(inherent_unequal_modideo2)[3] + knowledge*coef(inherent_unequal_modideo2)[4] + 
           median(conservatism)*coef(inherent_unequal_modideo2)[5] +
           median(education)*coef(inherent_unequal_modideo2)[6] +
           median(white)*coef(inherent_unequal_modideo2)[7] + median(female)*coef(inherent_unequal_modideo2)[8] + 
           median(protestant)*coef(inherent_unequal_modideo2)[9] + median(catholic)*coef(inherent_unequal_modideo2)[10] +
           median(jewish)*coef(inherent_unequal_modideo2)[11] + median(not_relig)*coef(inherent_unequal_modideo2)[12] +
           republican*ingroup_bias*coef(inherent_unequal_modideo2)[13] +
           republican*knowledge*coef(inherent_unequal_modideo2)[14] + 
           ingroup_bias*knowledge*coef(inherent_unequal_modideo2)[15] +
           republican*ingroup_bias*knowledge*coef(inherent_unequal_modideo2)[16]) %>%
  ggplot(aes(x = ingroup_bias, 
             y = preds, 
             colour = as.factor(republican)))+
  geom_smooth(aes(linetype = as.factor(knowledge)), method = "lm", se = FALSE)+
  ggtitle("Political Equality")+
  labs(x = "Affective Polarization", 
       y = "Support for Democratic Norm",
       col = "Partisan Identification",
       linetype = "Political Knowledge") +
  scale_x_continuous(limits = c(0,1), 
                     labels = c("Low",
                                "High"),
                     breaks = c(0,1)) +
  scale_colour_grey(breaks = c(0,1),
                    labels = c("Democrats", "Republicans"),
                    start = .1, end = .6)+
  scale_linetype_manual(values = c("dotted", "dashed", "solid"),
                        labels = c("Low", "Medium", "High"))+
  theme_bw()+
  theme(axis.text.y = element_blank(),
        axis.text.x = element_text(size=text.size),
        axis.title = element_text(size =text.size),
        axis.title.y = element_blank(), 
        axis.ticks.y = element_blank(),
        title = element_text(size = title.size),
        legend.text = element_text(size = legend.size)) +
  guides(lty = guide_legend(override.aes = list(col = 'black')))

fig8 <- partisans %>%
  filter(!is.na(republican) & !is.na(conservatism) &
           !is.na(knowledge) & !is.na(education) & !is.na(female) &
           !is.na(white) & !is.na(protestant) & !is.na(catholic) & 
           !is.na(jewish) & !is.na(not_relig) & !is.na(ingroup_bias)) %>%
  filter(knowledge == 1 | knowledge == 3 | knowledge == 5) %>%
  mutate(preds = coef(protest_modideo2)[1] + republican*coef(protest_modideo2)[2] +
           ingroup_bias*coef(protest_modideo2)[3] + knowledge*coef(protest_modideo2)[4] + 
           median(conservatism)*coef(protest_modideo2)[5] +
           median(education)*coef(protest_modideo2)[6] +
           median(white)*coef(protest_modideo2)[7] + median(female)*coef(protest_modideo2)[8] + 
           median(protestant)*coef(protest_modideo2)[9] + median(catholic)*coef(protest_modideo2)[10] +
           median(jewish)*coef(protest_modideo2)[11] + median(not_relig)*coef(protest_modideo2)[12] +
           republican*ingroup_bias*coef(protest_modideo2)[13] +
           republican*knowledge*coef(protest_modideo2)[14] + 
           ingroup_bias*knowledge*coef(protest_modideo2)[15] +
           republican*ingroup_bias*knowledge*coef(protest_modideo2)[16]) %>%
  ggplot(aes(x = ingroup_bias, 
             y = preds, 
             colour = as.factor(republican)))+
  geom_smooth(aes(linetype = as.factor(knowledge)), method = "lm", se = FALSE)+
  ggtitle("Prevent Protest")+
  labs(x = "Affective Polarization", 
       y = "Support for Democratic Norm",
       col = "Partisan Identification",
       linetype = "Political Knowledge") +
  scale_x_continuous(limits = c(0,1), 
                     labels = c("Low",
                                "High"),
                     breaks = c(0,1)) +
  scale_colour_grey(breaks = c(0,1),
                    labels = c("Democrats", "Republicans"),
                    start = .1, end = .6)+
  scale_linetype_manual(values = c("dotted", "dashed", "solid"),
                        labels = c("Low", "Medium", "High"))+
  theme_bw()+
  theme(axis.text.y = element_blank(),
        axis.text.x = element_text(size=text.size),
        axis.title = element_text(size =text.size),
        axis.title.y = element_blank(), 
        axis.ticks.y = element_blank(),
        title = element_text(size = title.size),
        legend.text = element_text(size = legend.size)) +
  guides(lty = guide_legend(override.aes = list(col = 'black')))  

#png("interactionplot.png", width = 775, height = 900, units = "px", type = "windows")

pdf(file = "figure1.pdf", 
    width = 8.45,
    height = 10.95)
ggarrange(fig1, fig2, fig3, fig4, fig5, fig6, fig7, fig8, 
          nrow = 4, ncol = 2, common.legend = TRUE, legend = "bottom")
dev.off()

###########################
## Analysis of TAPS Data ##
###########################

### TAPS Analysis
taps <- read.dta("TAPSdata2012.dta")

# Reading in demographic profile data
profile <- read.dta("profile.dta")

# merging the two together
taps <- merge(taps, profile, by.x = "WUSTLID", by.y = "wustlid")

# There are some duplicated column names, so removing duplicated columns
taps <- taps[, !duplicated(colnames(taps))]

taps <- taps %>%
  # Creating norms items from the TAPS data set
  mutate(exec_norm = ifelse(GIBRLAW4S6 == "strongly disagree", 5,
                        ifelse(GIBRLAW4S6 == "disagree", 4,
                               ifelse(GIBRLAW4S6 == "uncertain", 3,
                                      ifelse(GIBRLAW4S6 == "agree", 2,
                                             ifelse(GIBRLAW4S6 == "strongly agree", 1, NA)))))) %>%
  # Creating partisanship (Republican/Democrat) 
  mutate(PARTYID3S5 = ifelse(is.na(PARTYID3S5), 0, PARTYID3S5),
         PARTYID4S5 = ifelse(is.na(PARTYID4S5), 0, PARTYID4S5),
         rep = ifelse(PARTYID1S5 == "Republican" |
                        PARTYID3S5 == 3 |
                        PARTYID4S5 == 3, 1, 0),
         dem = ifelse(PARTYID1S5 == "Democrat" |
                        PARTYID3S5 == 2 |
                        PARTYID4S5 == 2, 1, 0)) %>%
  # Filtering to only partisans 
  filter(dem == 1 | rep == 1) %>%
  # Creating strength of pid variable to go from 1 to 3: proxy for affective polarization
  mutate(PARTYID2S5 = ifelse(is.na(PARTYID2S5), 0, PARTYID2S5),
         pidstrength = ifelse(PARTYID2S5 == 2, 2, 
                              ifelse(PARTYID2S5 == 3, 1, 
                                     ifelse(PARTYID2S5 == 0, 0,
                                            ifelse(PARTYID2S5 == 1, NA, NA))))) %>%
  # Creating covariates as close as possible to our study
  mutate(education = ifelse(educsp %in% c("Doctorate degree", 
                                          "Professional degree",
                                          "Master's degree"), 5, 
                            ifelse(educsp == "Bachelor's degree", 4,
                                   ifelse(educsp %in% c("Associate degree",
                                                        "Some college, but no degree"), 3,
                                          ifelse(educsp == "HIGH SCHOOL GRADUATE - high school DIPLOMA or the equivalent (GED)", 2,
                                                 ifelse(educsp == "Refused", NA, 1))))),
         know1 = ifelse(POLKNOW1S2 == "Republicans", 1, 0),
         know2 = ifelse(POLKNOW2S2 == "a two-thirds majority of both houses of Congress", 1, 0),
         know3 = ifelse(POLKNOW3S2 == "six years", 1, 0),
         know4 = ifelse(POLKNOW5S2 == "a filibuster", 1, 0),
         know5 = ifelse(POLKNOW6S2 == "Joseph Biden", 1, 0),
         knowledge = know1 + know2 + know3 + know4 + know5,
         white = ifelse(RACE1SP == "Refused" | HISPAN1SP == "Refused", NA,
                        ifelse(RACE1SP == "Yes" & HISPAN1SP == "No", 1, 0)),
         female = ifelse(gendersp == "Refused", NA,
                         ifelse(gendersp == "Female", 1, 0)),
         RLGNAS2 = ifelse(is.na(RLGN2AS2), "Refused", RLGN2AS2),
         protestant = ifelse(RLGNAS2 == 2, 1, 0),
         jewish = ifelse(RLGN1S2 == "Jewish", 1, 0),
         none = ifelse(RLGN1S2 == "not religious", 1, 0),
         RLGN = ifelse(is.na(RLGN2S2), "Refused", RLGN2S2),
         catholic = ifelse(RLGN == 5, 1, 0))

# Looking at support for the norm by party
taps %>%
  select(dem, rep, GIBRLAW4S6) %>%
  mutate(supportnorm = ifelse(GIBRLAW4S6 %in% c("strongly disagree", "disagree"), "Supports norm", 
                              ifelse(GIBRLAW4S6 == "Refused", NA, "Does not support norm"))) %>%
  na.omit() %>%
  count(supportnorm, dem) %>%
  group_by(dem) %>%
  mutate(percent = n/sum(n))

# Creating policy measure of conservatism in similar fashion to how we do in the paper
# First going to construct each measure such that higher values indicate more liberal
taps <- taps %>%
  mutate(policy1 = ifelse(PR1S3 == "cutting the deficit", 0, 
                          ifelse(PR1S3 == "creating more jobs", 1, NA)),
         policy2 = ifelse(PR2S3 == "curbing inflation", 0, 
                          ifelse(PR2S3 == "reducing unemployment", 1, NA)),
        policy3 = ifelse(PR3S3 == "increasing economic growth", 0, 
                         ifelse(PR3S3 == "reducing poverty", 1, NA)),
        policy4 = ifelse(PR4S3 == "reducing taxes", 0, 
                         ifelse(PR4S3 == "creating more jobs", 1, NA)),
        policy5 = ifelse(PR5S3 == "reducing the costs of doing business", 0, 
                         ifelse(PR5S3 == "protecting the environment", 1, NA)),
        policy6 = ifelse(JENNOEX9AS4 == "strongly oppose", 1,
                         ifelse(JENNOEX9AS4 == "somewhat oppose", 2,
                                ifelse(JENNOEX9AS4 == "neither support nor oppose", 3,
                                       ifelse(JENNOEX9AS4 == "somewhat support", 4,
                                              ifelse(JENNOEX9AS4 == "strongly support", 5, NA))))))


policy_pca2 <- taps %>%
  select(policy1, policy2, policy3, policy4, policy5, policy6) %>%
  na.omit() %>%
  prcomp(scale. = TRUE,
         center = TRUE)
summary(policy_pca2)
policy_pca2$rotation # all items on first dimension go in the same direction, which is good

# seeing if first dimension is conservatism or liberalism
taps %>%
  select(policy1, policy2, policy3, policy4, policy5, policy6, dem, rep) %>%
  na.omit() %>%
  summarize(cor = cor(policy_pca2$x[,1], dem)) # first dimension is negatively correlated with Dem. PID, so it is conservatism

# creating conservatism variable
taps <- taps %>%
  filter(!is.na(policy1) & !is.na(policy2) & !is.na(policy3) &
           !is.na(policy4) & !is.na(policy5) & !is.na(policy6)) %>%
  mutate(conservatism = scales::rescale(policy_pca2$x[,1]))

# Running linear model for similar norm item
exec_norm_mod <- lm(exec_norm ~ rep + pidstrength + rep*pidstrength +
               knowledge + conservatism + education + white + female + protestant + 
               catholic + jewish + none,
             data = taps)      
summary(exec_norm_mod)

stargazer(exec_norm_mod,
          type = "html",
          covariate.labels = c("Republican",
                               "Partisanship Strength",
                               "Political Knowledge",
                               "Conservatism",
                               "Education",
                               "Non-Hispanic White",
                               "Female",
                               "Religion: Protestant",
                               "Religion: Catholic",
                               "Religion: Jewish",
                               "Religion: None",
                               "Rep.* Affective Polarization"),
          out = "taps.htm")


######################################
## Re-Analysis of Lelkes & Westwood ##
######################################

# Analysis of Lelkes and Westwood with an interaction for party id
# Load in data 
load("study3_1.Rdata")

# reversing the endorse of tear gas item so that higher values indicate greater support for norm
dataTear$supportnorm <- 1-dataTear$agreedisagee

# Model support for norm from opposing party as a function of 
# affective polarization, party, their interaction, and controls
modelout1 <- lm(supportnorm ~ pid + affectivepolarization + pid*affectivepolarization + gender + race + 
                  education + income,
                subset(dataTear,Condition=='Opposing Partisans'))
summary(modelout1)

stargazer(modelout1,
          type = "html",
          covariate.labels = c("Republican",
                               "Affective Polarization",
                               "Political Knowledge",
                               "Female",
                               "Non-Hipanic White",
                               "College Education",
                               "Income: $30,000 - $59,999",
                               "Income: $60,000 - $79,999",
                               "Income: $80,000 or more",
                               "Rep.* Affective Polarization"),
          out = "lelkes.htm")


###########################################################
## Additional Code Needed for the Supplemental Materials ##
###########################################################

# NOTE: Below is all code necessary to replicate the appendix

# SI1: Sample characteristics
# age
norms %>%
  filter(age != 1) %>%
  count(age) %>%
  mutate(percent = prop.table(n))

# gender
norms %>%
  count(gender) %>%
  mutate(percent = prop.table(n))

# race/ethnicity
norms %>%
  count(race) %>%
  mutate(percet = prop.table(n))

# family income
norms %>%
  count(income) %>%
  mutate(percent = prop.table(n))

# education (associate's degree and some college combined)
norms %>%
  count(education) %>%
  mutate(percent = prop.table(n))

# also a part of SI1 is running the same models as in the main text of the paper,
# but adding in fixed effects for experimental condition. do that here
# and produce table with those fixed effects in it
methods_mod_fe <- lm(methods ~ republican + ingroup_bias + knowledge + conservatism +
                       education + white + female + protestant + 
                       catholic + jewish + not_relig + republican*ingroup_bias + 
                       as.factor(condition),
                     data = partisans)
summary(methods_mod_fe)

checks_mod_fe <- lm(checks ~ republican + ingroup_bias + knowledge + conservatism +
                      education + white + female + protestant + 
                      catholic + jewish + not_relig + republican*ingroup_bias + 
                      as.factor(condition),
                    data = partisans)
summary(checks_mod_fe)

exec_power_mod_fe <- lm(exec_power ~ republican + ingroup_bias + knowledge + conservatism +
                          education + white + female + protestant + 
                          catholic + jewish + not_relig + republican*ingroup_bias + 
                          as.factor(condition),
                        data = partisans)
summary(exec_power_mod_fe)

institu_respect_mod_fe <- lm(institu_respect ~ republican + ingroup_bias + knowledge + conservatism +
                               education + white + female + protestant + 
                               catholic + jewish + not_relig + republican*ingroup_bias + 
                               as.factor(condition),
                             data = partisans)
summary(institu_respect_mod_fe)

vote_mod_fe <- lm(vote ~ republican + ingroup_bias + knowledge + conservatism +
                    education + white + female + protestant + 
                    catholic + jewish + not_relig + republican*ingroup_bias + 
                    as.factor(condition),
                  data = partisans)
summary(vote_mod_fe)

political_voice_mod_fe <- lm(political_voice ~ republican + ingroup_bias + knowledge + conservatism +
                               education + white + female + protestant + 
                               catholic + jewish + not_relig + republican*ingroup_bias + 
                               as.factor(condition),
                             data = partisans)
summary(political_voice_mod_fe)

inherent_unequal_mod_fe <- lm(inherent_unequal ~ republican + ingroup_bias + knowledge + conservatism +
                                education + white + female + protestant + 
                                catholic + jewish + not_relig + republican*ingroup_bias + 
                                as.factor(condition),
                              data = partisans)
summary(inherent_unequal_mod_fe)

protest_mod_fe <- lm(protest ~ republican + ingroup_bias + knowledge + conservatism +
                       education + white + female + protestant + 
                       catholic + jewish + not_relig + republican*ingroup_bias + 
                       as.factor(condition),
                     data = partisans)
summary(protest_mod_fe)

stargazer(methods_mod_fe, checks_mod_fe, exec_power_mod_fe, institu_respect_mod_fe,
          vote_mod_fe, political_voice_mod_fe, inherent_unequal_mod_fe, protest_mod_fe,
          type = "html",
          covariate.labels = c("Republican",
                               "Affective Polarization",
                               "Political Knowledge",
                               "Conservatism",
                               "Education",
                               "Non-Hispanic White",
                               "Female",
                               "Religion: Protestant",
                               "Religion: Catholic",
                               "Religion: Jewish",
                               "Religion: None",
                               "Condition 2",
                               "Condition 3",
                               "Condition 4",
                               "Condition 5",
                               "Condition 6",
                               "Condition 7",
                               "Condition 8",
                               "Condition 9",
                               "Condition 10",
                               "Condition 11",
                               "Condition 12",
                               "Rep.* Affective Polarization"),
          out = "Aug17.htm")


# SI3: Showing distribution of answers on norms items

# plot for methods item
partisans %>%
  count(methods) %>%
  mutate(percent = prop.table(n)) %>%
  ggplot(aes(x = methods, y = percent))+
  geom_bar(stat = "identity") +
  labs(x = "Responses on item",
       y = "Percentage of respondents",
       title = "Item: 'I do not mind a politician's methods if he or she manages to get the right things done.'") +
  theme_bw() + 
  theme(axis.text = element_text(size = 20),
        axis.title = element_text(size = 20),
        title = element_text(size = 22))

# plot for checks item
partisans %>%
  count(checks) %>%
  mutate(percent = prop.table(n)) %>%
  ggplot(aes(x = checks, y = percent))+
  geom_bar(stat = "identity") +
  labs(x = "Responses on item",
       y = "Percentage of respondents",
       title = "Item: 'The executive, legislative, and judicial branches of government should keep one another from having\ntoo much power.'") +
  theme_bw() + 
  theme(axis.text = element_text(size = 20),
        axis.title = element_text(size = 20),
        title = element_text(size = 22))

# plot for exec power item
partisans %>% 
  count(exec_power) %>%
  mutate(percent = prop.table(n)) %>%
ggplot(aes(x = exec_power, y = percent))+
  geom_bar(stat = "identity") +
  labs(x = "Responses on item",
       y = "Percentage of respondents",
       title = "Item: 'When the country is in great danger, it is often necessary for political leaders to act boldly, even if this means\noverstepping the usual processes of government decision-making'") +
  theme_bw() + 
  theme(axis.text = element_text(size = 20),
        axis.title = element_text(size = 20),
        title = element_text(size = 22))

# plot for institutional respect item
partisans %>% 
  count(institu_respect) %>%
  mutate(percent = prop.table(n)) %>%
  ggplot(aes(x = institu_respect, y = percent))+
  geom_bar(stat = "identity") +
  labs(x = "Responses on item",
       y = "Percentage of respondents",
       title = "Item: 'It is important that the government treats other institutions with respect, such as news organizations,\nreligious communities, scientific groups, or business associations.'") +
  theme_bw() + 
  theme(axis.text = element_text(size = 20),
        axis.title = element_text(size = 20),
        title = element_text(size = 22))

# plot for vote item
partisans %>% 
  count(vote) %>%
  mutate(percent = prop.table(n)) %>%
  ggplot(aes(x = vote, y = percent))+
  geom_bar(stat = "identity") +
  labs(x = "Responses on item",
       y = "Percentage of respondents",
       title = "Item: 'People should be allowed to vote even if they are badly misinformed on basic facts about politics.'") +
  theme_bw() + 
  theme(axis.text = element_text(size = 20),
        axis.title = element_text(size = 20),
        title = element_text(size = 22))

# plot for political voice item
partisans %>% 
  count(political_voice) %>%
  mutate(percent = prop.table(n)) %>%
  ggplot(aes(x = political_voice, y = percent))+
  geom_bar(stat = "identity") +
  labs(x = "Responses on item",
       y = "Percentage of respondents",
       title = "Item: 'People who hate my way of life should still have a chance to talk in a public forum.'") +
  theme_bw() + 
  theme(axis.text = element_text(size = 20),
        axis.title = element_text(size = 20),
        title = element_text(size = 22))

# plot for inherent unequal item
partisans %>% 
  count(inherent_unequal) %>%
  mutate(percent = prop.table(n)) %>%
  ggplot(aes(x = inherent_unequal, y = percent))+
  geom_bar(stat = "identity") +
  labs(x = "Responses on item",
       y = "Percentage of respondents",
       title = "Item: 'We have to teach children that all people are created equal but almost everyone knows that some are\ninherently better than others.'") +
  theme_bw() + 
  theme(axis.text = element_text(size = 20),
        axis.title = element_text(size = 20),
        title = element_text(size = 22))

# plot for protest item
partisans %>% 
  count(protest) %>%
  mutate(percent = prop.table(n)) %>%
  ggplot(aes(x = protest, y = percent))+
  geom_bar(stat = "identity") +
  labs(x = "Responses on item",
       y = "Percentage of respondents",
       title = "Item: 'Some protests need to be prevented or stopped, even if they are completely peaceful.'") +
  theme_bw() + 
  theme(axis.text = element_text(size = 20),
        axis.title = element_text(size = 20),
        title = element_text(size = 22))

nato1 <- function(a){
  a <- ifelse(is.na(a), 1, a)
}

# SI8: Correlation matrix for norms measures
table2 <- partisans %>%
  select(methods, checks, exec_power, institu_respect, vote, inherent_unequal, political_voice, protest) %>%
  na.omit() %>%
  corrr::correlate() %>%
  mutate_all(.funs = nato1)

table2 <- as.data.frame(round(table2[,-1], digits = 2))

write_tableHTML(tableHTML(table2), "table2.html")


